package com.zj.leetcode.easy;

import java.util.HashMap;

/**
 * @program algorithm
 * @description:
 * @author: zhangbo
 * @create: 2022/01/08 17:48
 */
public class RansomNote {
    public static void main(String[] args) {
        Solution solution = new RansomNote().new Solution();
//        String ransomNote = "a", magazine = "b";
//        String ransomNote = "aa", magazine = "ab";
        String ransomNote = "aa", magazine = "aab";
        System.out.println(solution.canConstruct(ransomNote, magazine));
    }

    //leetcode submit region begin(Prohibit modification and deletion)
    class Solution {
        public boolean canConstruct(String ransomNote, String magazine) {

            HashMap<Character, Integer> map = new HashMap<>();

            for (int i = 0; i < magazine.length(); i++) {
                map.put(magazine.charAt(i), map.getOrDefault(magazine.charAt(i), 0) + 1);
            }

            for (int i = 0; i < ransomNote.length(); i++) {
                Integer orDefault = map.getOrDefault(ransomNote.charAt(i), -1);

                if (-1 == orDefault || orDefault == 0) {
                    return false;
                }
                map.put(ransomNote.charAt(i), orDefault - 1);
            }

            return true;
        }
    }
//leetcode submit region end(Prohibit modification and deletion)

}